<?php

/**
 * 默认控制器
 */
class Controller_Adminjqueryplugins extends Controller_Abstract
{
	protected $_backend = true;
		
	function actionIndex()
	{
    	if(isset($_GET['ope'])){
    		if($_GET['ope'] == 'delete'){
    			$id = $this->_context->get('id', null);
    			$deleteFiles = $this->_context->get('deleteFiles', 0);
    			if($deleteFiles == 1){
    				$deleteFiles = true;
    			}else{
    				$deleteFiles = false;
    			}
    			if($id){
    				Jqueryplugins::model()->delete($deleteFiles, array('id' => $id));
    			}
    		}
    	}
    	
    	$this->_view['jqueryplugins'] = Jqueryplugins::model()->find('*', null, null, null, array('replaceDescriptionVars' => true, 'mergeType' => true));
    	
    	$this->_viewname = '../admin/jqueryplugins/index';		
	}
	
	
	function actionAdd()
	{
		$formData = $this->_context->post('form', null);
		$jqPlugins = new Jqueryplugins();
		if($formData){
			$formFiles = empty($_FILES['form'])? null : $_FILES['form'];
			$now = time();
			$jqPlugins->attributes = array(
				'name' => empty($formData['name'])? '' : $formData['name'],
				'sketch' => empty($formData['sketch'])? '' : $formData['sketch'],
				'description' => empty($formData['description'])? '' : $formData['description'],
				'root_foldername' => empty($formData['root_foldername'])? '' : $formData['root_foldername'],
				'thumbnail' => empty($formFiles['name']['thumbnail'])? '' : Util::TimeFileName($formFiles['name']['thumbnail'], 'thumb', $now),
				'download_pack' => empty($formFiles['name']['download_pack'])? '' : Util::TimeFileName($formFiles['name']['download_pack'], 'download_pack', $now),
				'view_num' => 0,
				'type_id' => empty($formData['type_id'])? '' : $formData['type_id'],
				'created_date' => date('Y-m-d H:i:s', $now),
				'created_int' => $now
			);
			if($jqPlugins->attributesIdentity(array('name', 'root_foldername'))){
				if($jqPlugins->save()){
					$jqPlugins->saveFiles($formFiles);
				}
			}
		}
		
		$this->_view['jqPlugins'] = $jqPlugins;
    	$this->_view['jqueryplugins_type'] = Types::model()->find('*', array('tablename' => 'jqueryplugins'));
		$this->_viewname = '../admin/jqueryplugins/add';
	}
	
	function actionEdit()
	{
		$id = $this->_context->get('id', null);
		$jqPlugins = new Jqueryplugins();
		$jqPlugins->attributes = $jqPlugins->find('*', array('id' => $id), null, null, array('onlyOne' => true, 'htmlSpecialcharsPreContent' => false));
		if(!empty($jqPlugins->attributes)){
			$formData = $this->_context->post('form', null);
			if($formData){
				$formFiles = empty($_FILES['form'])? null : $_FILES['form'];
				$now = time();
				$jqPlugins->attributes = array_merge(
					array('id' => $id),
					$formData
				);
				if(!empty($formFiles['name']['thumbnail'])){
					$jqPlugins->attributes['thumbnail'] = Util::TimeFileName($formFiles['name']['thumbnail'], 'thumb', $now);
				}
				if(!empty($formFiles['name']['download_pack'])){
					$jqPlugins->attributes['download_pack'] = Util::TimeFileName($formFiles['name']['download_pack'], 'download_pack', $now);
				}
				//if($jqPlugins->attributesIdentity(array('name', 'root_foldername'))){
					if($jqPlugins->save()){
						$jqPlugins->saveFiles($formFiles);
					}
				//}
				
			}
			
			$this->_view['jqPlugins'] = $jqPlugins;
    		$this->_view['jqueryplugins_type'] = Types::model()->find('*', array('tablename' => 'jqueryplugins'));
			$this->_viewname = '../admin/jqueryplugins/edit';
		}
	}
	
}

